package com.android.jblm.commandcentral.adapters;

import android.app.Activity;
import android.content.Context;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import com.android.jblm.commandcentral.R;

import com.android.jblm.commandcentral.objects.Category;
import com.android.jblm.commandcentral.objects.Contact;
import com.android.jblm.commandcentral.objects.Header;

/**
 * Created by Brian 2014.
 */
public class HeaderAdapter extends ArrayAdapter<String> {

    private final Activity context;
    private final Header[] theHeaderList;
    private final Category[] theCategoryList;
    private final Contact[] theContactList;

    public HeaderAdapter(FragmentActivity context, Header[] list) {
        super(context, R.layout.list_header_item, R.id.headerItem);
        this.context = context;
        theCategoryList = null;
        theContactList = null;
        theHeaderList = list;
    }

    public HeaderAdapter(FragmentActivity context, Category[] list) {
        super(context,  R.layout.list_header_item, R.id.headerItem);
        this.context = context;
        theHeaderList = null;
        theContactList = null;
        theCategoryList = list;
    }

    public HeaderAdapter(FragmentActivity context, Contact[] list) {
        super(context, R.layout.list_header_item, R.id.headerItem);
        this.context = context;
        theHeaderList = null;
        theCategoryList = null;
        theContactList = list;
    }

    @Override
    public int getCount(){
        int size = 0;
        if(theHeaderList != null) {
            size = theHeaderList.length;
        } else if (theCategoryList != null){;
            size = theCategoryList.length;
        } else {
            size = theContactList.length;
        }

        return size;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        View theView = null;

        if(convertView == null){
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            theView = inflater.inflate(R.layout.list_header_item, null);

        } else {
            theView = convertView;
        }

        if(theHeaderList != null) {//show headers
            Log.w("HeaderAdapter", "Header View Created");
            TextView title = (TextView) theView.findViewById(R.id.headerItem);
            title.setText(theHeaderList[position].getName());
        } else if (theCategoryList != null){//show categories
            Log.w("HeaderAdapter", "Category View Created");
            TextView title = (TextView) theView.findViewById(R.id.headerItem);
            title.setText(theCategoryList[position].getTitle());
        } else {//show contacts
            Log.w("HeaderAdapter", "Contact View Created");
            TextView title = (TextView) theView.findViewById(R.id.headerItem);
            title.setText(theContactList[position].contactName);
        }

        return theView;
    }

}
